package com.example.Sao.Custom;

import android.util.Log;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: scan
 * @Package: com.example.MyApplication.Custom
 * @ClassName: RxTimer
 * @Description: 使用Rxjava提供定时服务
 * @Author: 阿毛
 * @CreateDate: 2020/2/21 15:47
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/2/21 15:47
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class RxTimer {
    private Disposable dis;
    private  final String TAG ="RxTimer";

    /** seconds秒后执行next操作
     *
     * @param seconds
     * @param next
     */
    public  void timer(long seconds,final IRxNext next){
        Observable.timer(seconds, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        dis = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        if(next!=null){
                            next.doNext(aLong,dis);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    /** seconds秒后执行next操作
     *
     * @param hours
     * @param next
     */
    public  void timerhour(long hours,final IRxNext next){
        Observable.timer(hours, TimeUnit.HOURS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        dis = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        if(next!=null){
                            next.doNext(aLong,dis);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    /** milliseconds秒后执行next操作
     *
     * @param milliseconds
     * @param next
     */
    public  void timer2(long milliseconds,final IRxNext next){
        Observable.timer(milliseconds, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        dis = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        if(next!=null){
                            next.doNext(aLong,dis);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    /** 每隔seconds秒后执行next操作
     *
     * @param seconds
     * @param next
     */
    public  void interval(long seconds,final IRxNext next){
        Observable.interval(seconds, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable disposable) {
                        dis=disposable;
                    }

                    @Override
                    public void onNext(Long number) {
                        if(next!=null){
                            next.doNext(number,dis);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 取消订阅
     */
    public  void cancel(){
        if(dis!=null&&!dis.isDisposed()){
            dis.dispose();
            Log.d(TAG,"定时器取消");
        }else {
            Log.d(TAG,"kong");
        }
    }

    public interface IRxNext{
        void doNext(long number,Disposable disposable);
    }
}

